//
//  MainTabBarViewController.swift
//  SwiftDemo
//
//  Created by hfzhangzhang on 2018/7/11.
//  Copyright © 2018年 hfzhangzhang. All rights reserved.
//

import UIKit



class MainTabBarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let customTabbar: MainTabBar = MainTabBar.init(frame: self.tabBar.frame)
        customTabbar.mainTabBardelegate = self;
        customTabbar.delegate = self;
        self.setValue(customTabbar, forKey: "tabBar")
        
        self.addChildVC()

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
extension MainTabBarViewController{
    
    func addChildVC(){
        self.addChildVC(childVC: HomeViewController(), childTitle: "UIKit", imageName: "tabbar_1", selectedImageName: "tabbar_selected_1")
        self.addChildVC(childVC: SecondViewController(), childTitle: "Second", imageName: "tabbar_2", selectedImageName: "tabbar_selected_2")
        self.addChildVC(childVC: ThirdViewController(), childTitle: "Third", imageName: "tabbar_3", selectedImageName: "tabbar_selected_3")
        self.addChildVC(childVC: FourthViewController(), childTitle: "Fourth", imageName: "tabbar_4", selectedImageName: "tabbar_selected_4")
    }

    private func addChildVC(childVC: UIViewController, childTitle: String, imageName: String, selectedImageName:String)
    {
        let navigation = UINavigationController(rootViewController: childVC)
        childVC.title = childTitle
        childVC.tabBarItem.tag = 1
        /**
         automatic    根据图片的使用环境和所处的绘图上下文自动调整渲染模式
         alwaysOriginal    始终绘制图片原始状态，不使用Tint Color。
         alwaysTemplate    始终根据Tint Color绘制图片，忽略图片的颜色信息。
         */
        childVC.tabBarItem.image = UIImage(named: imageName)?.withRenderingMode(.automatic)
        childVC.tabBarItem.selectedImage = UIImage(named: selectedImageName)?.withRenderingMode(.automatic)
        self.addChildViewController(navigation)
    }
    
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        
    }
    
}
extension MainTabBarViewController:MainTabBarDelegate
{
    func barButtonAction(sender: UIButton) {
        print("你点击了+号")
    }
}



